package com.lenovo.vod.player.callback;

import com.lenovo.vod.player.Constant;
import com.lenovo.vod.player.Engine;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

public class KeyListener implements View.OnKeyListener {
	private Engine mEngine;

	public KeyListener(Engine engine) {
		mEngine = engine;
	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		Log.d(Constant.TAG, "onKeyDown called[action: " + event.getAction()
				+ ", keycode: " + keyCode + "]");

		if (event.getAction() == KeyEvent.ACTION_UP) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_SPACE:
				mEngine.showControllerTimed();
				mEngine.pauseOrStart();
				break;
			case KeyEvent.KEYCODE_S:
				mEngine.showSetting();
				break;
			case KeyEvent.KEYCODE_A:
				mEngine.showOrHideController();
				break;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				mEngine.showControllerTimed();// start timer
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				mEngine.showControllerTimed();// start timer
				break;
			case KeyEvent.KEYCODE_ESCAPE:
				mEngine.destroy();
				break;
			case KeyEvent.KEYCODE_EQUALS:
				mEngine.setVolume(true);
				break;
			case KeyEvent.KEYCODE_MINUS:
				mEngine.setVolume(false);
				break;
			case KeyEvent.KEYCODE_M:
				mEngine.setMute();
				break;
			case KeyEvent.KEYCODE_PAGE_UP:
				mEngine.playPrevious();
				break;
			case KeyEvent.KEYCODE_PAGE_DOWN:
				mEngine.playNext();
				break;
			}

			mEngine.resetSeek();

		}
		else if (event.getAction() == KeyEvent.ACTION_DOWN) 
		{
			switch (keyCode) 
			{
			case KeyEvent.KEYCODE_DPAD_LEFT:
				mEngine.addKeyLeftCount();
				mEngine.fastBackWard();
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				mEngine.addKeyRightCount();
				mEngine.fastForWard();
				break;
			case KeyEvent.KEYCODE_PROG_GREEN :
				mEngine.seekTo(0);
				break;
			default:
				mEngine.resetSeek();
				break;
			}
		}
		else if(event.getAction() == KeyEvent.ACTION_MULTIPLE)
		{
			Log.i("VODPlayer", "It is the multiple key event");
		}
		
		return true;
	}
}
